/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package builder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author daz
 */
public class JarBuilder {

    private String pathFolder;
    private String jarName;
    private String newJarName;
    public String datFileName = "info.dat";
    private String desPath = "";
// 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    public void setPathFolder(String pathFolder) {
        this.pathFolder = pathFolder + File.separator;
    }

    public void setDesPath(String desPath) {
        this.desPath = desPath;
    }

    public void setJarName(String jarName) {
        this.jarName = jarName;
    }

    public void setNewJarName(String newJarName) {
        this.newJarName = newJarName + ".jar";
    }

    /**
     * Copy input to output stream
     */
    public void copyStream(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER)) != -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public void creatFile() {
        try {
            JarFile originalFile = new JarFile(pathFolder + jarName);
            JarOutputStream append = null;
            if (desPath.length() == 0) {
                append = new JarOutputStream(new FileOutputStream(pathFolder + newJarName));
            } else {
                append = new JarOutputStream(new FileOutputStream(desPath + newJarName));
            }
            //        // first, copy contents from existing war
            Enumeration<? extends JarEntry> entries = originalFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry e = entries.nextElement();
                System.out.println("copy: " + e.getName());
                if (!e.getName().equalsIgnoreCase(datFileName)) {
                    append.putNextEntry(e);
                    if (!e.isDirectory()) {
                        copyStream(originalFile.getInputStream(e), append);
                    }
                    append.closeEntry();
                }
            }
            // now append some extra content
            InputStream datInput = null;
            if (desPath.length() == 0) {
                datInput = new FileInputStream(pathFolder + datFileName);
            } else {
                datInput = new FileInputStream(desPath + datFileName);
            }
            JarEntry e = new JarEntry(datFileName);
            System.out.println("append: " + e.getName());
            append.putNextEntry(e);
            copyStream(datInput, append);
            append.closeEntry();
            datInput.close();
            //        // close
            originalFile.close();
            append.close();

        } catch (IOException ex) {
            Logger.getLogger(JarBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
